package com.lwk.service.impl;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.transaction.annotation.Transactional;

import com.lwk.dao.StudentDao;
import com.lwk.domain.Student;
import com.lwk.service.StudentService;
import com.lwk.utils.PageBean;

public class StudentServiceImpl implements StudentService {

	private StudentDao studentDao;

	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

	@Transactional(readOnly = true)
	public PageBean getPageBean(DetachedCriteria dc, Integer page, Integer rows) {
		// 1 调用Dao查询总记录数
		Integer totalCount = studentDao.getTotalCount(dc);
		// 2 创建PageBean对象
		PageBean pb = new PageBean(page, totalCount, rows);
		// 3 调用Dao查询分页列表数据

		List<Student> list = studentDao.getPageList(dc, pb.getStart(), pb.getPageSize());
		// 4 列表数据放入pageBean中.并返回
		pb.setList(list);
		return pb;
	}

	/*@Transactional(readOnly = false)
	public void resetting() {
		studentDao.resetting();
	}*/

	@Transactional(readOnly = true)
	public List<Student> login(DetachedCriteria dc) {
		return studentDao.login(dc);
	}

	@Override
	@Transactional(readOnly = false)
	public Integer getByStudentNumber(DetachedCriteria dc, Integer number) {
		try {
			Student student = studentDao.getByStudentNumber(dc);
			if(student.getOne()==1){
				return 3;
			}else if (student.getTwo()==1){
				return 3;
			}else if (student.getThree()==1){
				return 3;
			}else if (student.getFour()==1){
				return 3;
			}else if (student.getFive()==1){
				return 3;
			}else if (student.getSix()==1){
				return 3;
			}else if (student.getSeven()==1){
				return 3;
			}else if (student.getEight()==1){
				return 3;
			}
			if (number == 1){
				student.setOne(1);
			}
			int i = student.getPtc();
			i++;
			student.setPtc(i);
			studentDao.update(student);
			return 1;
		}catch (Exception e){
			return 2;
		}

	}

	@Transactional(readOnly = true)
	public Student getById(Long sid) {
		Student student = studentDao.getById(sid);
		return student;
	}

	@Transactional(readOnly = false)
	public void delete(Student student1) {
		studentDao.delete(student1);

	}

	@Transactional(readOnly = false)
	public void saveOrUpdate(Student student) {
		studentDao.saveOrUpdate(student);

	}

	@Transactional(readOnly = true)
	public void export(ServletOutputStream outputStream, DetachedCriteria dc) {
		// 根据查询条件获取供应商/客户列表
		List<Student> studentList = studentDao.getList(dc);
		// 创建excel工作簿
		HSSFWorkbook wk = new HSSFWorkbook();
		HSSFSheet sheet = null;
		sheet = wk.createSheet("学生信息");

		// 写入表头
		HSSFRow row = sheet.createRow(0);
		// 定义好每一列的标题
		String[] headerNames = { "学号", "学生姓名", "学年", "所属学院", "所属专业", "班级", "打卡次数" };
		// 指定每一列的宽度
		int[] columnWidths = { 5000, 3000, 2000, 7000, 4000, 2000,3000 };
		HSSFCell cell = null;
		for (int i = 0; i < headerNames.length; i++) {
			cell = row.createCell(i);
			cell.setCellValue(headerNames[i]);
			sheet.setColumnWidth(i, columnWidths[i]);
		}

		// 写入内容
		int i = 1;
		for (Student student : studentList) {
			row = sheet.createRow(i);
			// 必须按照hderarNames的顺序来
			row.createCell(0).setCellValue(student.getStudentNumber());// 学号
			row.createCell(1).setCellValue(student.getStudentName());// 学生姓名
			//row.createCell(2).setCellValue(student.getPassword());// 密码
			row.createCell(2).setCellValue(student.getYear());// 学年
			row.createCell(3).setCellValue(student.getCollege());// 所属学院
			row.createCell(4).setCellValue(student.getMajor());// 所属专业
			row.createCell(5).setCellValue(student.getClassAndGrade());// 所属学院
			row.createCell(6).setCellValue(student.getPtc());// 打卡次数
			i++;
		}
		try {
			// 写入到输出流中
			wk.write(outputStream);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

		}

	}

	@Transactional(readOnly = false)
	public void doImport(FileInputStream is) {
		HSSFWorkbook wb = null;
		try {
			wb = new HSSFWorkbook(is);
			HSSFSheet sheet = wb.getSheetAt(0);

			/*
			 * String type = ""; if ("供应商".equals(sheet.getSheetName())) { type =
			 * Supplier.TYPE_SUPPLIER; } else if ("客户".equals(sheet.getSheetName())) { type
			 * = Supplier.TYPE_CUSTOMER; } else { throw new ErpException("工作表名称不正确"); }
			 */
			// 读取数据
			// 最后一行的行号

			int lastRow = sheet.getLastRowNum();
			Student student = null;
			for (int i = 1; i <= lastRow; i++) {
				student = new Student();
				sheet.getRow(i).getCell(0).setCellType(HSSFCell.CELL_TYPE_STRING);
				student.setStudentNumber(sheet.getRow(i).getCell(0).getStringCellValue());
				//student.setStudentNumber(sheet.getRow(i).getCell(0).getStringCellValue());
				student.setStudentName(sheet.getRow(i).getCell(1).getStringCellValue());// 地址
				student.setPassword(sheet.getRow(i).getCell(0).getStringCellValue());// 联系人
				student.setYear(sheet.getRow(i).getCell(2).getStringCellValue());// 电话
				student.setCollege(sheet.getRow(i).getCell(3).getStringCellValue());// Email
				student.setMajor(sheet.getRow(i).getCell(4).getStringCellValue());// Email
				student.setClassAndGrade(sheet.getRow(i).getCell(5).getStringCellValue());// Email
				/*HSSFCell cell = row.getCell(keyIndex);
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);*/
				sheet.getRow(i).getCell(6).setCellType(HSSFCell.CELL_TYPE_STRING);
				student.setPtc(Integer.parseInt(sheet.getRow(i).getCell(6).getStringCellValue()));// Email
				/*
				 * if (list.size() == 0) { supplierDao.add(supplier); }
				 */
				studentDao.save(student);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


}
